package com.ef.hana;

import com.ef.utils.FileUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: ErFeng_V
 * @ProjectName: parse_file
 * @Date: 2021/12/10/10:39 上午
 * @Description: hana字段转换成hive字段
 */
public class HanaParse {

    public List<String> fileds=new ArrayList<>();
    public List parse(String filepath,String encode)  {
        List<String> list=new ArrayList<>();
        String[] contents=FileUtils.getContentByLine(filepath,encode).split("\n");
        //因为第一段是字段名，所以从第二段开始
        for(int i=1;i<contents.length;i++){
            //System.out.println(contents[i]);
            String []fileds=contents[i].split("\t");
            getFields(fileds[0]);
            //字段名
            String line=fileds[0]+"\t";
            if(fileds[2].equals(HanaField.NVARCHAR) || fileds[2].equals(HanaField.CLOB) ||  fileds[2].equals(HanaField.VARCHAR)){
                line+=HiveField.STRING+"\t";

            }else if(fileds[2].equals(HanaField.DECIMAL)){
                line+=HiveField.DECIMAL+ (fileds[3]==null?"": "("+fileds[3]+","+(fileds[4].equals("[NULL]") ?0:fileds[4])+")")+"\t";

            }else if(fileds[2].equals(HanaField.DATE)){
                line+=HiveField.DATE+"\t";
            }else if(fileds[2].equals(HanaField.TIMESTAMP) || fileds[2].equals(HanaField.SECONDDATE)){
                line+=HiveField.TIMESTAMP+"\t";
            }

            line+=HiveField.COMMENT+"\t"+"'"+fileds[fileds.length-1]+"'";
            list.add(line);

        }
        return list;
    }
    public List parse(String filepath){
        return parse(filepath,null);
    }


    private void getFields(String field){
        fileds.add(field);
    }

    public String selectFileds(String tablename){

        String line=HiveField.SELECT+"\n"+fileds.get(0);
        for(int i=1;i<fileds.size();i++){
            line+=","+fileds.get(i)+"\n";
        }
        line+=",to_char(CURRENT_TIMESTAMP,'yyyy-mm-dd HH24:mi:ss') AS ETL_TIME\n"+"from\t"+tablename;

        System.out.println("JOB_HANA_BACKUP_"+tablename.replace(".","_"));
        System.out.println();
        return line;
    }
}
